(function($){
	var shadow=$("<div class='yanhx_shadow'></div>");
	var loadShadow=$("<div class='yanhx_loadshadow'></div>");
	var winElem=$("<div class='yanhx_window'><div class='window_tool' callFunc='mousedown:drag'>"+
								"<span class='title'></span><a href='javascript:void(0)' class='ctrl' callFunc='click:doWinCancel'></a>"+
								"</div><div class='window_content'></div></div>");
	var content=winElem.children(".window_content");
	var tool=winElem.children(".window_tool");
	var titleSpan=tool.children(".title");
	var ctrl=tool.children(".ctrl");
	var $doc=$(document);
	var winUqKey="_windowPrefix_";
	var winKey=parseInt(Math.random()*100);
	var alertElem=$("<div class='yanhx_alert'>"+winElem.html()+"<div class='alertBtn'></div></div>");
	alertElem.find(".ctrl").attr("callFunc","click:doAlertClose").attr("val","n");
	//提示框
	var alertCtrl="<button class='btn' callFunc='click:doAlertClose'>确定</button></div>";
	//确认框
	var confirmCtrl="<button class='btn' callFunc='click:doAlertClose' val='y' >是</button></div><button class='btn' callFunc='click:doAlertClose' val='n' >否</button></div>";
	
	var alertShadow=$("<div class='yanhx_shadow'></div>");
	var loadElem=$("<div class='yanhx_loading'></div>");
	var warnElem="<div class='yanhx_warn'></div>";
	//窗口 根据当前实例创建 多例模式
	var openWinTemp="<div class='yanhx_window' style='z-index:997'><div class='window_tool' callFunc='mousedown:drag'>"+
	"<span class='title'></span><a href='javascript:void(0)' class='ctrl'></a>"+
	"</div><div class='window_content'></div>" +
	"<div style='text-align:right;padding-top:3px'><button class='btn confirm'>确定</button></div></div>";
	$.warn=function(mes){
		var elem=$(warnElem).html(mes).appendTo(document.body);
		var t=window.setTimeout(function(){
			elem.remove();
			elem=null;
			window.clearTimeout(t);
			t=null;
		},1500);
	};
	function winFunc(){
		
	}
	winFunc.prototype={
		//elem:内容 key:实例 closeType:关闭方式 （删除，隐藏）
		open:function(elem,key,closeType){
			this.key=key;
			this.closeFunc=closeType?".del":".hide";
			this.win=$(openWinTemp).appendTo(document.body);
			this.win.find("a").attr("callFunc","click:"+key+this.closeFunc);
			this.win.find("button.confirm").attr("callFunc","click:"+key+".callback;"+key+this.closeFunc);
			this.content=this.win.find(".window_content");
			this.content.append(elem);
			this.show();
		},
		show:function(){
			this.win.show();
			this.win.css({
				top:event.pageY,
				left:event.pageX
			});
		},
		hide:function(){
			this.win.hide();
		},
		del:function(){
			this.win.remove();
			yanhx.exec("common.del",this.key);
		},
		callback:function(){
			
			
		}
	};
	yanhx.addComponent("myWin",{
			openSrc:null,
			showload:function(){
				//已显示则不处理
				if(loadElem.get(0).style.display=="block"){
					return;
				}
				this.initShadow(loadShadow);
				loadElem.show();
				this.setPosition(loadElem);	
			},
			hideload:function(){
				loadElem.hide();
				loadShadow.hide();
			},
			openAlert:function(param){
				alertElem.data("alertParam",param);
				if(!alertElem.parent().length){
					yanhx.exec("myWin.init");	
				}
				var con=alertElem.find(".window_content").html(param.content);
				alertElem.show();
				yanhx.exec("myWin.setPosition",alertElem);
				if(param.title){
					alertElem.find(".title").text(param.title);
				}
				else{
					alertElem.find(".title").text("");
				}	
				alertShadow.css("z-index",alertElem.css("z-index")-1);
				yanhx.exec("myWin.initShadow",alertShadow);	
			},
			alert:function(param){
				this.openAlert(param);
				var btn=alertElem.children(".alertBtn").html(alertCtrl);
			},
			confirm:function(param){
				this.openAlert(param);
				var btn=alertElem.children(".alertBtn").html(confirmCtrl);
			},
			init:function(){
				loadShadow.appendTo(document.body);
				shadow.appendTo(document.body);
				winElem.appendTo(document.body);
				alertElem.appendTo(document.body);
				alertShadow.appendTo(document.body);
				$(document.body).append(loadElem);
			},
			closeWin:function(){
				shadow.hide();
				winElem.hide();
				content.empty();
				if(this.openParam&&this.openParam.onclose){
					this.openParam.onclose();
				}
				this.openParam=null;
			},	
			closeAlert:function(choose){
				var param=alertElem.data("alertParam");
				alertShadow.hide();
				alertElem.hide();
				if(param.func){
					param.func();
				}
				else if(choose&&param[choose]){
					param[choose]();
				}
				param=null;
				alertElem.data("alertParam",null);
			},		
			fillOpenElem:function(data){
				yanhx.exec("fillHandle.fill",{elem:content,data:data});
			},
			removeParam:function(){
				this.openParam=null;
			},
			saveParam:function(param){
				if(!param.backParam){
					param.backParam={};
				}
				this.openParam=param;
			},
			getParam:function(){
				return this.openParam;
			},
			//param {elem 内容，title 标题,(afterOpen,data)open后回调函数 }
			open:function(param){
				yanhx.exec("myWin.saveParam",param);
				if(!winElem.parent().length){
					yanhx.exec("myWin.init");	
				}
				if(param.model){
					yanhx.exec("myWin.initShadow",shadow);	
				}
				content.empty();
				param.elem.appendTo(content).show();
				
				winElem.show();
				//调整zIndex
				if(param.src){
					var fromWin=param.src.parentUtil("div.yanhx_window");
					if(fromWin&&fromWin.length){
						yanhx.exec("common.setIndex",{
							from:fromWin,
							open:winElem
						});
					}
				}
				yanhx.exec("myWin.setPosition",winElem);	
				param.title=param.title?param.title:'';
				titleSpan.text(param.title);
				if(param.afterOpen){
					param.afterOpen.call(param.data);
				}
				content.find("input").each(function(){
					$(this).bind("blur",yanhx.find("fillHandle").valid);
				});
			},
			//src 事件源 callback回调 isDelete 关闭时是否删除 默认隐藏
			openWin:function(param){
				var id=yanhx.exec("idHandle.get",param.src);
				var key=id+"_handle_";
				var handle=yanhx.find(key);
				if(!handle){
					var inst=new winFunc();
					//设置回调函数
					inst.callback=param.callback;
					inst.open(param.elem,key,param.isDelete);
					var fromWin=param.src.parentUtil("div.yanhx_window");
					if(fromWin&&fromWin.length){
						yanhx.exec("common.setIndex",{
							from:fromWin,
							open:inst.win
						});
					}
					yanhx.addComponent(key,inst);
				}else{
					//已创建对象
					handle.show();
				}
			},
			initShadow:function(elem){
					var docInfo=yanhx.exec("common.docOffset");
					elem.css({
						width:docInfo.width,
						height:docInfo.height	
					});
					elem.show();
			},
			setPosition:function(elem){
					var docInfo=yanhx.exec("common.docOffset");
					var temp={
						w:elem.width(),
						h:elem.height()
					};
					elem.css({
						left:(docInfo.client_w-temp.w)/2+docInfo.left,
						top:(docInfo.client_h-temp.h)/3+docInfo.top,
					});
			},
			isChange:function(param){
					var oldKey=yanhx.exec("myWin.getUqKey",param.old);	
					var newKey=yanhx.exec("myWin.getUqKey",param.old);
					if(oldKey!=newKey){
						return true;	
					}	
					else{
						return false;	
					}
			},
			getUqKey:function(elem){
					var key=elem.attr("winUqKey");
					if(!key){
						key=winUqKey+(winKey++)+parseInt(Math.random()*10000);
					}
					return key;
			},
			callback:function(data){
				if(this.openParam.callback)
					this.openParam.callback.call(this.openParam.src,data);
			}	
			
	});
	$.fn.openWin=function(param){
		param.elem=$(this);
		yanhx.exec("myWin.openWin",param);
	};
	$.fn.modelWin=function(param){
		if(!param)
			param={};
		//默认为模态窗口 非模态窗口 调用openWin
		param.model=true;
		param.elem=$(this);
		yanhx.exec("myWin.open",param);
	};
})(jQuery);